/* Copyright (c) 2010, mitsuhisa.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of the misuhisa.net nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sstream>
#include <algorithm>
#include "Array.hpp"
#include "Context.hpp"
#include "String.hpp"

namespace nyu {

namespace {

struct UnrefFunc {
    void operator()(Value v) const {
        if (is_object(v) && !is_null(v))
            value_to_object(v)->unref();
    }
};

}

Array::Array(Value proto, size_t size) :
        GeneralObject(proto), elms_(size) {
}

Array::~Array() {
    std::for_each(elms_.begin(), elms_.end(), UnrefFunc());
}

String* Array::toString(Context& c) {
    // TODO
    std::ostringstream ost;
    ost << "ARRAY(" << this << ")";
    return new DynamicString(ost.str());
}

Value Array::opGetItem(Value idx, Context& c, bool is_tail) {
    if (is_int(idx)) {
        int_type i = value_to_int(idx);
        if (i < 0 || elms_.size() <= i) {
            // TODO exception
            return NULL_VALUE;
        }
        return elms_[i];
    }

    return GeneralObject::opGetItem(idx, c, is_tail);
}

int_type Array::length(Context& c) {
    return elms_.size();
}

Value Array::push(Context& c) {
    for (int i = 1, j = c.currentArgc(); i < j; i++) {
        Value v = c.currentArgv(i);
        if (is_object(v) && !is_null(v))
            value_to_object(v)->ref();
        elms_.push_back(v);
    }

    ref();

    return object_to_value(this);
}

Value Array::pop(Context& c) {
    if (elms_.empty())
        return NULL_VALUE;
    Value v = elms_.back();
    elms_.pop_back();
    return v;
}

Value Array::each(Context& c) {
    Value callable = c.currentArgv(1);
    if (!is_object(callable) || is_null(callable))
        return NULL_VALUE;

    Value ret = NULL_VALUE;

    typedef std::deque<Value>::iterator iter;
    for (iter it = elms_.begin(); it != elms_.end(); ++it) {
        if (is_object(ret) && !is_null(ret))
            value_to_object(ret)->unref();
        ret = c.call(callable, *it);
    }

    return ret;
}

void Array::eachChildren(const EachChildrenFunc& fn) {
    GeneralObject::eachChildren(fn);

    typedef std::deque<Value>::iterator iter;
    for (iter it = elms_.begin(); it != elms_.end(); ++it) {
        if (is_object(*it) && !is_null(*it))
            fn(value_to_object(*it));
    }
}

}
